<?php

namespace app\index\controller\source;

use app\index\model\UM as UM;
use app\index\service\SourceCollectService;
use ShidianUtil\Thinkphp\Response\ApiResponse;
use ShidianUtil\Utils\CheckUtil;
use think\APP;
use think\Request as Request;

/**
 * SourceCollect 控制器
 * Class SourceCollect
 * @package app\index\controller\source;
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037509
 * @see \app\index\validate\source\SourceCollect
 */
class SourceCollect
{
    // 请求参数
    private Request $request;

    // service
    private SourceCollectService $service;

    public function __construct(Request $request, SourceCollectService $sourceCollectService)
    {
        $this->request = $request;
        $this->service = $sourceCollectService;
    }

    /**
     * 收藏线索
     * @param UM $user
     * @return ApiResponse
     * @url /source/source_collect/save
     */
    public function save(UM $user): ApiResponse
    {
        $request = $this->request;

        $id = $request->param('id');

        if (!CheckUtil::isId($id)) {
            return ApiResponse::fail('缺少id参数');
        }

        $this->service->add($user, $id);

        return ApiResponse::success(null);
    }

    /**
     * 取消收藏
     * @param UM $user
     * @return ApiResponse
     * @url /source/source_collect/delete
     */
    public function delete(UM $user): ApiResponse
    {
        $request = $this->request;

        $id = $request->param('id');

        if (!CheckUtil::isId($id)) {
            return ApiResponse::fail('缺少id参数');
        }

        $this->service->rm($user, $id);

        return ApiResponse::success(null);
    }
}
